package com.ruoyi.commodity.service.impl;


import com.ruoyi.commodity.domain.Commodity;
import com.ruoyi.commodity.mapper.CommodityMapper;
import com.ruoyi.commodity.service.ICommodityService;
import com.ruoyi.common.core.constant.CommodityConstants;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CommodityServiceImpl implements ICommodityService {

    @Autowired
    private CommodityMapper commodityMapper;

    @Override
    public boolean checkCommodityNameUnique(Commodity commodity) {
        Long commodityId =  StringUtils.isNull(commodity.getCommodityId()) ? -1 : commodity.getCommodityId();
        Commodity commodityCheck =  commodityMapper.checkCommodityNameUnique(commodity.getName());
        if (StringUtils.isNotNull(commodityCheck) && commodityCheck.getCommodityId().longValue() != commodityId.longValue()) {
            return CommodityConstants.NOT_UNIQUE;
        }
        return CommodityConstants.UNIQUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertCommodity(Commodity commodity) {
        return commodityMapper.insertCommodity(commodity);
    }

    @Override
    public List<Commodity> selectCommodityList(Commodity commodity) {
        return commodityMapper.selectCommodityList(commodity);
    }
}
